<template>
	<view class="poster_container">
		<!-- <image :src="" mode=""></image> -->
		<canvas class="auto" style="margin-top: 25rpx;" :style="{ width: canvasW + 'px', height: canvasH + 'px' }" canvas-id="myCanvas"
		 id="myCanvas"></canvas>
		<view class="poster_bottom back display align-items justify-content">
			<view class="poster_bottom_top auto" @click="saveImage">保存图片</view>
		</view>
	</view>
</template>
<script>
	var _this
	export default {
		components: {},
		data() {
			return {
				canvasW: 0,
				canvasH: 0,
				SystemInfo: {},
				goodsImg: {},
				ewmImg: {},
				title: '',
				price: '158.00',
				Oldprice: '350.00',
				img: '',
				infoList: {},
				imgurl: ""
			}
		},
		async onLoad(option) {
			this.imgUrl = this.$imgUrl
			var infoList = option.info.replace(/""/g, "");
			infoList = JSON.parse(decodeURIComponent(infoList))
			this.infoList = infoList
			this.title = infoList.goods_name
			this.SystemInfo = await this.getSystemInfo();
			this.goodsImg = await this.getImageInfo(this.$imgUrl + this.infoList.goods_image);
			this.ewmImg = await this.getImageInfo(this.$imgUrl + this.infoList.mpqr);
			this.canvasW = 360;
			this.canvasH = 500;

			if (this.goodsImg.errMsg == 'getImageInfo:ok' && this.ewmImg.errMsg == 'getImageInfo:ok' && this.SystemInfo.errMsg ==
				'getSystemInfo:ok') {
				console.log('ok')
				uni.showToast({
					icon: 'loading',
					title: '加载中',
				});
				setTimeout(() => {
					var ctx = uni.createCanvasContext('myCanvas', this);
					ctx.setFillStyle('#B1CCD6')
					ctx.fillRect(0, 0, 360, 500)
					// logo 二维码
					ctx.drawImage(this.goodsImg.path, 25, 25, 310, 307)
					ctx.drawImage(this.ewmImg.path, this.canvasW - 115, this.canvasW, 90, 90)
					// 商品名称
					ctx.setFontSize(16);
					ctx.setFillStyle('#fff');
					let _strlineW = 0;
					let _strLastIndex = 0; //每次开始截取的字符串的索引
					let _strHeight = this.canvasW + 60; //绘制字体距离canvas顶部的初始高度
					let _num = 1;
					for (let i = 0; i < this.title.length; i++) {
						_strlineW += ctx.measureText(this.title[i]).width;
						if (_strlineW > this.canvasW - 155) {
							if (_num == 2 && 2) {
								ctx.fillText(this.title.substring(_strLastIndex, i - 5) + '...', 25, _strHeight);
								_strlineW = 0;
								_strLastIndex = i;
								_num++;
								break;
							} else {
								ctx.fillText(this.title.substring(_strLastIndex, i), 25, _strHeight);
								_strlineW = 0;
								_strHeight += 20;
								_strLastIndex = i;
								_num++;
							}
						} else if (i == this.title.length - 1) {
							ctx.fillText(this.title.substring(_strLastIndex, i + 1), 25, _strHeight);
							_strlineW = 0;
						}
					}



					// 扫码识别
					ctx.setFontSize(14)
					ctx.setFillStyle('#fff')
					ctx.fillText('扫码识别', 262, this.canvasW + 110);


					//商品价格
					ctx.setFontSize(28)
					ctx.setFillStyle('#fff')
					ctx.setShadow(10, 50, 50, '#fff')
					ctx.fillText('￥' + this.infoList.price, 24, this.canvasW + 25);
					ctx.draw(false, (ret) => {
						console.log(ret)
						uni.canvasToTempFilePath({
							canvasId: 'myCanvas',
							fileType: 'jpg',
							success: function(res) {
								uni.hideLoading()
								uni.setStorageSync('tempFilePath', res.tempFilePath)
							},
						})
					});
				}, 1500)
			} else {
				console.log('err')
			}
		},
		methods: {
			//获取图片
			getEwmImg(id){
				// getEwmImg
				console.log(id)
				var data = {
					id:id
				}
				this.$myRequest.post(this.$api.default.getGoodsImage, data).then(res => {
					if (res.code == '200') {
						that.addressList = res.data.data
						callback && callback()
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none"
						})
					}
				})
			},
			// 获取图片信息
			getImageInfo(image) {
				return new Promise((req, rej) => {
					uni.getImageInfo({
						src: image,
						success: function(res) {
							req(res)
						},
					});
				})
			},
			setImage(img) {
				console.log(img)
			},
			// 获取设备信息
			getSystemInfo() {

				return new Promise((req, rej) => {
					uni.getSystemInfo({
						success: function(res) {
							req(res)
						}
					});
				})
			},
			saveImage() {
				let that = this
				uni.saveImageToPhotosAlbum({
					filePath: uni.getStorageSync('tempFilePath'),
					success: function(res) {
						uni.showToast({
							title: '图片已保存'
						})
					},
					fail: function(err) {
						console.log(err)
					}
				})


			},
		}
	}
</script>

<style lang="scss" scoped>
	.poster_container {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		width: 100%;
		height: 100%;
		margin: auto;
		background: #EFF0F2;


		.poster_bottom {
			height: 132rpx;
			padding: 35rpx 0;
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
			margin: auto;
			border-radius: 10rpx 10rpx 0px 0px;

			.poster_bottom_top {
				width: 440rpx;
				height: 83rpx;
				line-height:83rpx;
				background: #294D7C;
				border-radius: 4rpx;
				text-align: center;
				font-size: 26rpx;
				color: #fff;
			}

			.poster_bottom_bt {
				width: 440rpx;
				height: 72rpx;
				line-height: 72rpx;
				text-align: center;
				font-size: 26rpx;
				color: #A2A5AA;
			}
		}
	}
</style>
